﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using BITS.PreCompiler.CSharp.Attributes;
using Roslyn.Compilers.CSharp;

namespace BITS.PreCompiler.CSharp.Syntax
{
    /// <summary>
    /// Represents a attribute abstraction.
    /// </summary>
    public partial class AttributeDeclaration : BaseSyntaxNode<AttributeDeclaration, AttributeListSyntax>
        , IEnumerable<Attribute>
        , IEnumerable<IEnumerable<Attribute>>
    {
        # region • Fields & Properties •

        /// <summary>
        /// Holds the parent.
        /// </summary>
        private IBaseSyntaxNode parent;

        /// <summary>
        /// Gets the parent.
        /// </summary>
        public override IBaseSyntaxNode Parent
        {
            get { return this.parent; }
            internal set { this.parent = value; }
        }

        /// <summary>
        /// Gets or sets the target specifier syntax.
        /// </summary>
        /// <value>
        /// The target specifier syntax.
        /// </value>
        public AttributeTargetSpecifierSyntax TargetSpecifierSyntax { get; set; }

        public AttributeTargets Target
        {
            get
            {
                return (AttributeTargets)System.Enum.Parse(typeof(AttributeTargets),
                    this.TargetSpecifierSyntax.Identifier.ToFullString());
            }
            set
            {
                this.TargetSpecifierSyntax = Roslyn.Compilers.CSharp.Syntax.AttributeTargetSpecifier(
                    Roslyn.Compilers.CSharp.Syntax.ParseToken(value.ToString().ToLower()));
            }
        }

        /// <summary>
        /// Holds the attributes
        /// </summary>
        private readonly List<Attribute> attributes
            = new List<Attribute>();

        /// <summary>
        /// Gets the attributes.
        /// </summary>
        public IEnumerable<Attribute> Attributes
        {
            get { return this.attributes; }
        }

        /// <summary>
        /// Gets the argument list.
        /// </summary>
        private SeparatedSyntaxList<AttributeSyntax> AttributeList
        {
            get
            {
                if (!this.Attributes.Any())
                    return default(SeparatedSyntaxList<AttributeSyntax>);

                return Roslyn.Compilers.CSharp.Syntax.SeparatedList(
                    nodes:      this.Attributes.Select(x => x.GetSyntaxNode()),
                    separators: Enumerable.Repeat(Roslyn.Compilers.CSharp.Syntax.Token(SyntaxKind.None),
                        this.Attributes.Count() - 1));
            }
        }

        public static readonly AttributeDeclaration IgnorDeclaration =
            new AttributeDeclaration(new Attribute(
                typeof(CompilerIgnorTypeAttribute).Name));

        public virtual Boolean Ignore
        {
            get
            {
                return this.GetSyntaxNode().EquivalentTo(
                    IgnorDeclaration.GetSyntaxNode());
            }
        }

        # endregion Fields & Properties

        # region • Ctor's •

        /// <summary>
        /// Initializes a new instance of the <see cref="AttributeDeclaration"/> attribute.
        /// </summary>
        internal AttributeDeclaration() { }

        /// <summary>
        /// Initializes a new instance of the <see cref="AttributeDeclaration"/> attribute.
        /// </summary>
        /// <param name="parent">The parent.</param>
        public AttributeDeclaration(IBaseSyntaxNode parent)
        {
            this.parent = parent;
        }

        public AttributeDeclaration(AttributeTargets target, IBaseSyntaxNode parent = null)
            : this(parent)
        {
            this.Target = target;
        }

        public AttributeDeclaration(AttributeTargets target, Attribute attribute, IBaseSyntaxNode parent = null)
            : this(attribute, parent)
        {
            this.Target = target;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AttributeDeclaration"/> class.
        /// </summary>
        /// <param name="attribute">The attribute.</param>
        /// <param name="parent">The parent.</param>
        public AttributeDeclaration(Attribute attribute, IBaseSyntaxNode parent = null)
            : this(parent)
        {
            if (attribute == null)
                return;

            this.attributes.Add(attribute);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AttributeDeclaration"/> attribute.
        /// </summary>
        /// <param name="syntax">The attribute declaration syntax.</param>
        /// <param name="parent">The parent.</param>
        public AttributeDeclaration(AttributeListSyntax syntax, IBaseSyntaxNode parent = null)
            : this(parent)
        {
            if (syntax == null)
                return;

            this.TargetSpecifierSyntax = syntax.Target;

            this.attributes = syntax.Attributes
                .Select(x => new Attribute(x)).ToList();
        }

        # endregion Ctor's

        # region • Methods •

        /// <summary>
        /// Builds the class decleration.
        /// </summary>
        /// <returns></returns>
        private AttributeListSyntax BuildAttributeDeclarationSyntax()
        {
            return Roslyn.Compilers.CSharp.Syntax.AttributeList(
                target:     this.TargetSpecifierSyntax,
                attributes: this.AttributeList);
        }

        public override AttributeListSyntax GetSyntaxNode()
        {
            return this.BuildAttributeDeclarationSyntax();
        }

        /// <summary>
        /// Adds the specified attribute.
        /// </summary>
        /// <param name="attribute">The attribute.</param>
        public AttributeDeclaration Add(Attribute attribute)
        {
            if (attribute == null)
                return this;

            if (attribute.Name == null)
                throw new NullReferenceException("attribute.Name");

            attribute.Parent = this;
            this.attributes.Add(attribute);

            return this;
        }

        public void Add(IEnumerable<Attribute> attributeList)
        {
            if (attributeList == null)
                return;

            foreach (var attribute in attributeList)
                this.Add(attribute);
        }

        # region • Methods - IEnumerable •

        /// <summary>
        /// Returns an enumerator that iterates through the collection.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> 
        /// that can be used to iterate through the collection.
        /// </returns>
        IEnumerator<Attribute> IEnumerable<Attribute>.GetEnumerator()
        {
            return this.Attributes.GetEnumerator();
        }

        /// <summary>
        /// Returns an enumerator that iterates through the collection.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> 
        /// that can be used to iterate through the collection.
        /// </returns>
        IEnumerator<IEnumerable<Attribute>> IEnumerable<IEnumerable<Attribute>>.GetEnumerator()
        {
            yield return this.Attributes;
        }

        /// <summary>
        /// Returns an enumerator that iterates through a collection.
        /// </summary>
        /// <returns>
        /// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
        /// </returns>
        /// <filterpriority>2</filterpriority>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.Attributes.GetEnumerator();
        }

        # endregion Methods - IEnumerable

        # endregion Methods
    }
}
